package static_block;

/**
 * @Description: 饿汉模式中的静态代码块
 * @Author: WangBlue
 * @Create: 2024-09-23 14:37
 * @Version: 1.0
 **/

public class StaticBlock {
    // 私有化构造器， 防止外部进行实例化构造对象
    private StaticBlock(){ }

    // 静态代码块，在类被加载时执行
    private static StaticBlock instance;
    static {
        instance = new StaticBlock();
    }

    // 静态方法，获取实例对象
    public static StaticBlock getInstance(){
        return instance;
    }
}

class Test {
    public static void main(String[] args) {
        StaticBlock instance1 = StaticBlock.getInstance();
        StaticBlock instance2 = StaticBlock.getInstance();
        if (instance1 == instance2) {
            System.out.println("两个实例对象是同一个");
        }
    }
}
